﻿using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Collab.Services.Remote.Entities
{
    [DataContract(Namespace = "http://collab.codeplex.com/2010/10/Schemas")]
    public class Change : IExtensibleDataObject, IEquatable<Change>
    {
        [XmlAttribute]
        [DataMember(Order = 1, IsRequired = true)]
        public int OldStart { get; set; }

        [XmlAttribute]
        [DataMember(Order = 2, IsRequired = true)]
        public int OldEnd { get; set; }

        [XmlAttribute]
        [DataMember(Order = 3, IsRequired = true)]
        public int Delta { get; set; }

        [XmlAttribute]
        [DataMember(Order = 4, IsRequired = true)]
        public string Text { get; set; }

        /// <summary>
        /// Do not use!
        /// </summary>
        public Change()
        {
            throw new InvalidOperationException("Only used for serialization");
        }

        public Change(int oldStart, int oldEnd, int delta, string text)
        {
            Debug.Assert(oldStart <= oldEnd);

            this.OldStart = oldStart;
            this.OldEnd = oldEnd;
            this.Text = text;
            this.Delta = delta;
        }

        #region IExtensibleDataObject Members

        public ExtensionDataObject ExtensionData { get; set; }

        #endregion

        #region IEquatable<Change> Members

        public bool Equals(Change other)
        {
            return this.OldStart == other.OldStart &&
                   this.OldEnd == other.OldEnd &&
                   this.Delta == other.Delta &&
                   this.Text == other.Text;
        }

        #endregion
    }
}
